﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;



public partial class AbAssist_AbAssist : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request.QueryString.HasKeys())
            {
                if (Request.QueryString["listid"] != String.Empty)
                    Session["listid"] = Request.QueryString["listid"];
                else
                    Session["listid"] = 1;


                if (Request.QueryString["dnis"] != null)
                {
                    Session["dnis"] = Request.QueryString["dnis"];
                }
                else
                {
                    Session["dnis"] = "9999999999";
                }

                Session["servername"] = Request.QueryString["servername"];
                Session["projectname"] = Request.QueryString["projectname"];
                Session["campaignname"] = Request.QueryString["campaignname"];
                Session["agentid"] = Request.QueryString["agentid"];
                Session["agentfirst"] = Request.QueryString["agentfirst"];
                Session["agentlast"] = Request.QueryString["agentlast"];


                Session["App"] = new DataAccess();
            }

        }
    }

    public double total;

    //POST LOGIC - For Step or Start pages
    //Add if statements as you add pages
    //For Next and Finish pages it is best to use the index in the if statements as below
    protected void WebWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {

        int page = WebWizard.ActiveStepIndex;

        if (page == WebWizard.WizardSteps.IndexOf(this.Intro))
        {
            if (cbMainProd.Checked)
            {
                ((DataAccess)Session["App"]).PutOrderItem(477, int.Parse(ddlMainProdQty.SelectedValue));
            }
        }



    }

    //POST LOGIC for disposition pages
    //this is for pages that contain a disposition button
    protected void WebWizard_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        int page = WebWizard.ActiveStepIndex;
        
        if (page == WebWizard.WizardSteps.IndexOf(this.UpSells))
        {
            if (cbUpsell1.Checked)
            {
                ((DataAccess)Session["App"]).PutOrderItem(478, int.Parse(ddlUpsell1.SelectedValue));
            }
            if (cbUpsell2.Checked)
            {
                ((DataAccess)Session["App"]).PutOrderItem(479, int.Parse(ddlUpsell2.SelectedValue));
            }
            if (cbUpsell3.Checked)
            {
                ((DataAccess)Session["App"]).PutOrderItem(480, int.Parse(ddlUpsell3.SelectedValue));
            }
            if (cbUpsell4.Checked)
            {
                ((DataAccess)Session["App"]).PutOrderItem(481, int.Parse(ddlUpsell4.SelectedValue));
            }
            if (cbUpsell5.Checked)
            {
                ((DataAccess)Session["App"]).PutOrderItem(482, int.Parse(ddlUpsell5.SelectedValue));
            }
            if (cbRushShipping.Checked)
            {
                ((DataAccess)Session["App"]).PutOrderItem(483, 1);
            }
        }

        if (page == WebWizard.WizardSteps.IndexOf(this.Disposition))
        {
        }


    }

    //PRE-LOGIC
    //Add if statements as needed for post-logic
    //You can use the Step ID name instead of the index as used in Next and Finish logic.
    protected void WebWizard_ActiveStepChanged(object sender, EventArgs e)
    {


        int page = WebWizard.ActiveStepIndex;

        if (page == WebWizard.WizardSteps.IndexOf(this.Intro))
        {
            btnSubmit.Enabled = true;
            if (((DataAccess)Session["App"]) != null)
                ((DataAccess)Session["App"]).RemoveOrderItem(477);
        }
        else
            btnSubmit.Enabled = false;

        if (page == WebWizard.WizardSteps.IndexOf(this.UpSells))
        {

            if (!cbMainProd.Checked || lblDataSaved.Text == "")
            {
                WebWizard.ActiveStepIndex = WebWizard.WizardSteps.IndexOf(this.Intro);
                lblError.Text = "Please enter customer info";
            }

            ((DataAccess)Session["App"]).RemoveOrderItem(478);
            ((DataAccess)Session["App"]).RemoveOrderItem(479);
            ((DataAccess)Session["App"]).RemoveOrderItem(480);
            ((DataAccess)Session["App"]).RemoveOrderItem(481);
            ((DataAccess)Session["App"]).RemoveOrderItem(482);
            ((DataAccess)Session["App"]).RemoveOrderItem(483);


        }

        if (page == WebWizard.WizardSteps.IndexOf(this.Disposition))
        {
            ((DataAccess)Session["App"]).PutCallData("webusercode", "01");

            ((DataAccess)Session["App"]).CommitOrder();
            ((DataAccess)Session["App"]).CommitCallData();


            //foreach (System.Data.DataRow r in ((DataAccess)Session["App"]).GetProducts().Rows)
            //{
            //    ((DataAccess)Session["App"]).RemoveOrderItem(Convert.ToInt32(r["productid"]));
            //}

            Response.Redirect("../DispositionComplete.aspx");
        }

    }

    public void ToggleVisibility(DropDownList ddl)
    {
        if (ddl.Visible)
            ddl.Visible = false;
        else
        {
            ddl.SelectedIndex = 0;
            ddl.Visible = true;
        }
    }

    protected void btnSubmit_Click(object sender, System.EventArgs e)
    {
        lblDataSaved.Text = "";
        lblError.Text = "";

        bool bCustValid = false;


        bCustValid = !(string.IsNullOrEmpty(tbFirstName.Text) ||
            string.IsNullOrEmpty(tbLastName.Text) ||
            string.IsNullOrEmpty(tbAddr.Text) ||
            string.IsNullOrEmpty(tbZip.Text) ||
            string.IsNullOrEmpty(ddlCity.SelectedItem.Text) ||
            string.IsNullOrEmpty(ddlState.SelectedItem.Text) ||
            string.IsNullOrEmpty(ddlCountry.SelectedItem.Text)) &&
            (ddlCity.SelectedIndex > 0);


        if (bCustValid)
        {
            ((DataAccess)Session["App"]).PutCallData("firstname", tbFirstName.Text);
            ((DataAccess)Session["App"]).PutCallData("lastname", tbLastName.Text);
            ((DataAccess)Session["App"]).PutCallData("address1", tbAddr.Text);
            if (!string.IsNullOrEmpty(tbAddr2.Text))
                ((DataAccess)Session["App"]).PutCallData("address2", tbAddr2.Text);
            else
                ((DataAccess)Session["App"]).PutCallData("address2", "");
            ((DataAccess)Session["App"]).PutCallData("Zip", tbZip.Text);
            ((DataAccess)Session["App"]).PutCallData("City", ddlCity.SelectedItem.Text);
            ((DataAccess)Session["App"]).PutCallData("State", ddlState.SelectedItem.Text);
            ((DataAccess)Session["App"]).PutCallData("Country", ddlCountry.SelectedItem.Text);

            lblDataSaved.Text = "Successfully Saved";

        }
        else
        {
            lblError.Text += "Enter Valid Customer Data<br/>";
            lblDataSaved.Text = "";
        }

        if ((!string.IsNullOrEmpty(tbPhone.Text)) && ((tbPhone.Text.Length != 10) || !(System.Text.RegularExpressions.Regex.Match(tbPhone.Text, @"^[1-9]\d{2}[1-9]\d{6}$").Success)))
        {
            lblError.Text += "Invalid Phone<br/>";
            lblDataSaved.Text = "";
        }
        else
        {
            if (!string.IsNullOrEmpty(tbPhone.Text))
                ((DataAccess)Session["App"]).PutCallData("PrimaryPhone", tbPhone.Text);
        }
    }

    protected void btnSearchZip_Click(object sender, EventArgs e)
    {
        ValidateZip(tbZip.Text.ToUpper(), ddlCity, ddlState, ddlCountry);
    }

    protected void ValidateZip(string strZip, DropDownList ddl_City, DropDownList ddl_State, DropDownList ddl_Country)
    {

        ddl_City.Items.Clear();
        ddl_State.Items.Clear();
        ddl_City.Enabled = true;

        if (System.Text.RegularExpressions.Regex.IsMatch(strZip.ToUpper(), @"(^\d{5}$)|(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)"))
        {
            try
            {
                com.codebump.GeoPlaces g = new com.codebump.GeoPlaces();

                com.codebump.AuthenticationHeader authHeader = new com.codebump.AuthenticationHeader();
                authHeader.SessionID = "6JV2ZA4lgG1YRR+HG+JU27QWk9zI3rpRWY2iG2mzNPcdieEQXfQv0OUY0EZ+pOTxhwI1NCuF7b2oY+U/bozy0mhjFHmpaXvb";
                g.AuthenticationHeaderValue = authHeader;

                com.codebump.GeoPlaceDistance[] places = g.GetPlacesInside(strZip, "");

                for (int i = 0; i < places.Length; i++)
                {
                    com.codebump.GeoPlaceDistance place = places[i];
                    ListItem li = new ListItem(place.ToPlace, place.ToState + "," + place.ToPlace);
                    ddl_City.Items.Add(li);
                }

                ddl_State.Items.Add(ddl_City.SelectedItem.Value.Split(",".ToCharArray())[0]);

                //ddlCity.SelectedIndex = 0;

                if (System.Text.RegularExpressions.Regex.IsMatch(strZip.ToUpper(), @"(^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$)"))
                {
                    ddl_Country.SelectedValue = "CANADA";
                }
                else
                {
                    ddl_Country.SelectedValue = "USA";
                }

                if (ddl_City.Items.Count <= 0)
                {
                    ddl_City.Items.Add("No Cities Found");
                    ddl_City.Enabled = false;
                }
                else
                {
                    ddl_City.Items.Insert(0, "Select a City");
                    ddl_City.SelectedIndex = 0;
                }
            }
            catch
            {
                ddl_City.Items.Add("No Cities Found");
                ddl_City.Enabled = false;
            }
        }
        else
        {
            ddl_City.Items.Add("Invalid Zip");
            ddl_City.Enabled = false;
        }
    }


    protected void cbMainProd_CheckedChanged(object sender, EventArgs e)
    {
        Page.SetFocus(cbMainProd);
        ddlMainProdQty.Visible = !ddlMainProdQty.Visible;
    }
    protected void cbUpsell1_CheckedChanged(object sender, EventArgs e)
    {
        Page.SetFocus(pnlUpsell1);
        ToggleVisibility(ddlUpsell1);
    }
    protected void cbUpsell2_CheckedChanged(object sender, EventArgs e)
    {
        Page.SetFocus(pnlUpsell2);
        ToggleVisibility(ddlUpsell2);
    }
    protected void cbUpsell3_CheckedChanged(object sender, EventArgs e)
    {
        Page.SetFocus(pnlUpsell3);
        ToggleVisibility(ddlUpsell3);
    }
    protected void cbUpsell4_CheckedChanged(object sender, EventArgs e)
    {
        Page.SetFocus(pnlUpsell4);
        ToggleVisibility(ddlUpsell4);
    }
    protected void cbUpsell5_CheckedChanged(object sender, EventArgs e)
    {
        Page.SetFocus(pnlUpsell5);
        ToggleVisibility(ddlUpsell5);
    }
}

